<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

        const URL = 'http://192.168.26.39/todo/list/1';

        // const xhr = new XMLHttpRequest();
        // xhr.open('get', URL);
        // xhr.send();
        // xhr.onreadystatechange = function () {
        //     if (xhr.readyState === 4 && xhr.status === 200) {
        //         const res = JSON.parse(xhr.responseText);
        //         console.log(res);
        //     }
        // };

        // 面试题: 手写axios
        function bxios(config) {
            const {
                method = 'get',
                url,
            } = config;
            return new Promise((resolve, reject) => {
                const xhr = new XMLHttpRequest();
                xhr.open(method, url);
                xhr.send();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4) {
                        if (xhr.status === 200) {
                            const res = JSON.parse(xhr.responseText);
                            resolve(res);
                        } else {
                            reject(xhr);
                        }
                    }
                };
            });
        }

        bxios({
            // method: 'get',
            url: URL,
        })
        .then(res => {
            console.log(res);
        })
        .catch(err => {
            console.log(err);
        });

    </script>
</body>
</html>
